﻿using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;
using System.Globalization;
namespace Extensions
{

    /// <summary>
    /// Summary description for ClassExtensions
    /// </summary>
    public static class ClassExtensions
    {
        [Obsolete("This method is obsolete. Use MShop.Convert.ToNullableDouble()")]
        public static int ToInt32(this string val)
        {
            int result;
            int.TryParse(val, out result);
            return result;
        }
        [Obsolete("This method is obsolete. Use MShop.Convert.ToNullableDouble()")]
        public static double? GetDoubleOrNull(this string val)
        {
            double v;
            if (double.TryParse(val, out v))
                return v;
            else
                return null;
        }
        [Obsolete("This method is obsolete. Use MShop.Convert.ToMoneyString()")]
        public static string FormatPrice(this Page page, decimal price)
        {
            return FormatPrice(price);
        }
        [Obsolete("This method is obsolete. Use MShop.Convert.ToMoneyString()")]
        public static string ObjectToMoneyString(this object price)
        {
            decimal p;
            decimal.TryParse(price.ToString(), out p);
            return FormatPrice(p);
        }
        [Obsolete("This method is obsolete. Use MShop.Convert.ToMoneyString()")]
        public static string ToMoneyString(this decimal price)
        {
            return FormatPrice(price);
        }
        private static string FormatPrice(decimal price)
        {
            CultureInfo ci = new CultureInfo("ru-ru");
            return ((decimal)price).ToString("c", ci);
        }
        public static void RequestLogin(this Page page)
        {
            page.Response.Redirect(FormsAuthentication.LoginUrl +
               "?ReturnUrl=" + page.Request.Url.PathAndQuery);
        }
        [Obsolete("This method is obsolete. Use MShop.Convert.ToNullableDecimal()")]
        public static decimal? GetDecimalOrNull(this string val)
        {
            decimal v;
            if (decimal.TryParse(val, out v))
                return v;
            else
                return null;
        }

        public static string GetShortText(this string description, int textLimit)
        {
            if (description.Length > textLimit)
            {
                description = string.Format("{0}...", description.Substring(0, textLimit));
            }
            return description;
        }

        public static string GetQueryStringNavigateUrl(this Page page, string queryStringField, string parameterValue, HttpRequest request)
        {
            string result;
            System.Text.StringBuilder builder = new System.Text.StringBuilder();
            builder.Append(request.Path);
            builder.Append("?");
            foreach (string str2 in request.QueryString.AllKeys)
            {
                if (!str2.Equals(queryStringField, StringComparison.OrdinalIgnoreCase))
                {
                    builder.Append(HttpUtility.UrlEncode(str2));
                    builder.Append("=");
                    builder.Append(HttpUtility.UrlEncode(request.QueryString[str2]));
                    builder.Append("&");
                }
            }
            builder.Append(queryStringField);
            builder.Append("=");
            result = builder.ToString();
            return (result + parameterValue.ToString(System.Globalization.CultureInfo.InvariantCulture));
        }
    }
    
}
